package com.demo.page9101.controller.news;

import com.demo.common.entity.ColumnManagement;
import com.demo.common.entity.Meta;
import com.demo.common.entity.News;
import com.demo.page9101.feign.columnManagement.ColumnManagementFeign;
import com.demo.page9101.feign.news.NewsFeign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻动态
 * 作者：张佑威
 * 补充：孙佳佳
 * 分配任务：张改
 * 插入数据：张佑威
 * 接口文档：贾冠博
 */
@RestController
@CrossOrigin
public class NewsController {
    Map<String, Object> meta = new HashMap<>();

    Map<String, Object> dates = new HashMap<>();

    @Resource
    private NewsFeign newsFeign;

    @Resource
    private ColumnManagementFeign columnManagementFeign;

    /**
     * 新闻分页
     * @param pagenum
     * @return
     */
    @GetMapping("/News/selectAll")
    public Map search(@RequestParam(value = "pagenum") Integer pagenum){
        List<News> search = newsFeign.search(pagenum);
        int totalSize = newsFeign.search(0).size();
        int totalPage = totalSize % 4 ==0 ? totalSize / 4 : totalSize / 4 + 1;
        System.out.println("totalSize = " + totalSize);
        System.out.println("totalPage = " + totalPage);
        dates.put("totalSize",totalSize);
        dates.put("pagenum", pagenum);
        dates.put("totalPage",totalPage);
        dates.put("news", newsFeign.search( pagenum));
        meta.put("date", dates);
        if (search.size()>0) {
            meta.put("meta", new Meta("获取成功", "200"));
        }else {
            meta.put("meta", new Meta("获取失败", "404"));
        }

        return meta;
    }

    @GetMapping("/News/id/{NewId}")
    public Map selectById(@PathVariable("NewId") Integer NewId){
        News neww = newsFeign.selectById(NewId);
        System.out.println(neww);
        dates.put("new",neww);
        meta.put("date", dates);
        if (neww!=null){
            meta.put("meta", new Meta("获取成功", "200"));
        }else {
            meta.put("meta", new Meta("获取失败", "404"));
        }
        return meta;
    }

    //查询新闻子类及文章
    @GetMapping("/News/getnews")
    public Map newsManager(@RequestParam(value = "id",required = true) Integer id,
                           @RequestParam(value = "pagenum",required = false,defaultValue = "1") Integer pagenum){
        List<News> news = newsFeign.newsproduct(id, -1);
        Map<String, Object> dates = new HashMap<>();
        Map<String, Object> meta = new HashMap<>();
        int totalSize = news.size();
        int totalPage = totalSize % 4 ==0 ? totalSize / 4 : totalSize / 4 + 1;
        ColumnManagement columnManagement = columnManagementFeign.oneManagement(id);
        String columnName = columnManagement.getColumnName();
        dates.put("columnName",columnName);
        dates.put("news",newsFeign.newsproduct(id, pagenum));

        dates.put("pagenum",pagenum);
        dates.put("totalPage",totalPage);
        meta.put("date", dates);
        if (news.size()>0){
            meta.put("meta", new Meta("获取成功", "200"));
        }else {
            meta.put("meta", new Meta("获取失败", "404"));
        }

        return meta;
    }
}
